<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
 }
 
 public function adminlogin()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_admin_check_database');
   #$this->form_validation->set_rules('admin', 'admin', 'trim|require|xss_clean');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.&nbsp; User redirected to login page
     $data['_scripts'] = '';
	 $data['_styles'] = '';
	 $data['head'] = '';
	 $data['schulname'] = 'Berufskolleg Uerdingen';
	 $data['navi'] = '';
	 $data['login'] = $this->load->view('login/adminlogin_view', $data, TRUE);
	 $data['content'] = '';
	 $data['footer'] = $this->load->view('templates/footer', $data, TRUE);
	
	 $this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
     #$this->load->view('login_view');
   }
   else
   {
     //Go to private area
     redirect('home', 'refresh');
     //$this->load->view('klausuren/view');
     //redirect('/klausuren_controller');
   } 
 }
 
  function admin_check_database($password)
  {
   //Field validation succeeded.&nbsp; Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->user->adminlogin($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
  }
  
   public function lehrerlogin()
   {
	   //This method will have the credentials validation
	   $this->load->library('form_validation');
	
	   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_lehrer_check_database');
	   #$this->form_validation->set_rules('admin', 'admin', 'trim|require|xss_clean');
	
	   if($this->form_validation->run() == FALSE)
	   {
	     //Field validation failed.&nbsp; User redirected to login page
	     $data['_scripts'] = '';
		 $data['_styles'] = '';
		 $data['head'] = '';
		 $data['schulname'] = 'Berufskolleg Uerdingen';
		 $data['navi'] = '';
		 $data['login'] = $this->load->view('login/lehrerlogin_view', $data, TRUE);
		 $data['content'] = '';
		 $data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		 $this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	     #$this->load->view('login_view');
	   }
	   else
	   {
	     //Go to private area
	     redirect('home/lehrerindex', 'refresh');
	   } 
   }

   function lehrer_check_database($password)
   {
	   //Field validation succeeded.&nbsp; Validate against database
	   $username = $this->input->post('username');
	
	   //query the database
	   $result = $this->user->lehrerlogin($username, $password);
	
	   if($result)
	   {
	     $sess_array = array();
	     foreach($result as $row)
	     {
	       $sess_array = array(
	         'id' => $row->id,
	         'username' => $row->username
	       );
	       $this->session->set_userdata('logged_in', $sess_array);
	     }
	     return TRUE;
	   }
	   else
	   {
	     $this->form_validation->set_message('check_database', 'Invalid username or password');
	     return false;
	   }
   }
   
   public function schuelerlogin()
   {
	   //This method will have the credentials validation
	   $this->load->library('form_validation');
	
	   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_schueler_check_database');
	   #$this->form_validation->set_rules('admin', 'admin', 'trim|require|xss_clean');
	
	   if($this->form_validation->run() == FALSE)
	   {
	     //Field validation failed.&nbsp; User redirected to login page
	     $data['_scripts'] = '';
		 $data['_styles'] = '';
		 $data['head'] = '';
		 $data['schulname'] = 'Berufskolleg Uerdingen';
		 $data['navi'] = '';
		 $data['login'] = $this->load->view('login/schuelerlogin_view', $data, TRUE);
		 $data['content'] = '';
		 $data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		 $this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	     #$this->load->view('login_view');
	   }
	   else
	   {
	     //Go to private area
	     redirect('home/schuelerindex', 'refresh');
	   } 
   }

   function schueler_check_database($password)
   {
	   //Field validation succeeded.&nbsp; Validate against database
	   $username = $this->input->post('username');
	
	   //query the database
	   $result = $this->user->schuelerlogin($username, $password);
	
	   if($result)
	   {
	     $sess_array = array();
	     foreach($result as $row)
	     {
	       $sess_array = array(
	         'id' => $row->id,
	         'username' => $row->username
	       );
	       $this->session->set_userdata('logged_in', $sess_array);
	     }
	     return TRUE;
	   }
	   else
	   {
	     $this->form_validation->set_message('check_database', 'Invalid username or password');
	     return false;
	   }
   }
   
   
}
?>
